from redis import Redis

# cache = Redis(host='127.0.0.1', port='6379', password='bitailian123456', decode_responses=True)
cache = Redis(host='127.0.0.1', port='6379', decode_responses=True)
# cache = Redis(host='172.17.0.1', port='6379', decode_responses=True)
pipe = cache.pipeline()
# cache = Redis(host='192.168.0.99', port='6379', decode_responses=True, password='123456')


def sets(key, value, timeout):  # 5分钟
    return cache.set(key, value, ex=timeout)


def get(key):
    return cache.get(key)


def delete(key):
    return cache.delete(key)


def set_last(key, value):
    return cache.set(key, value)


def set_list(key, lists, timeout):  # 缓存列表
    cache.lpush(str(key), *lists)
    cache.expire(key, timeout)
    return True


def set_list_last(key, lists):  # 缓存列表
    cache.lpush(str(key), *lists)
    # cache.expire(key, )
    return True


def get_list(key):  # 获取列表
    return cache.lrange(key, -1, 0)


def create_goods_cache(goods_list, times):
    """
    创建商品缓存
    :param goods_list:  商品列表
    :param times:   时间
    :return:
    """
    lua_script = """
        redis.call('SELECT', 1);
        local _goodsIdS = ARGV[1];
        local _splitChar = ARGV[2];
        local _timeout = ARGV[3];

        for _id in string.gmatch(_goodsIdS, '(.-)'.. _splitChar) do
        redis.call('SETEX', 'goods:'.. _id, _timeout, '');
        end
        return 'OK';
      """
    cmd = cache.register_script(lua_script)
    res = cmd(keys=[], args=[goods_list + ',', ',', times])
    print(res)
    return res


def create_token_cache(user_id, token, times):
    """
    生成token
    :param user_id:  用户id
    :param token:  token
    :param times:   时间
    :return:
    """
    lua_script = """
        redis.call('SELECT', 1);

        local _user_id = ARGV[1];

        -- 
        local _orz = 'qiangdan:u:'.. _user_id;

        -- 
        local _access_token = redis.call('GET', _orz);
        if _access_token then
        redis.call('DEL', _access_token);
        end;

        _access_token = 'qiangdan:u:'.. _user_id ..':t:'.. ARGV[2];

        -- 
        local _timeout = ARGV[3];

        -- 
        redis.call('SETEX', _orz, _timeout, _access_token);

        -- 
        redis.call('HMSET', _access_token, 'user_id', _user_id);
        -- access_token: 3600(s)
        redis.call('EXPIRE', _access_token, _timeout);

        return 'OK';
      """
    cmd = cache.register_script(lua_script)
    res = cmd(keys=[], args=[user_id, token, times])
    print(res)
    return res


def snap_up_cache(user_id, token, goods_id):
    """
    抢购
    :param user_id:  用户id
    :param token:   token
    :param goods_id:   商品id
    :return:
    """
    lua_script = """
        redis.call('SELECT', 1);

        local _user_id = ARGV[1];
        local _token = ARGV[2];
        local _goods_id = ARGV[3];

        --
        local _orz = 'qiangdan:u:'.. _user_id ..':t:'.. _token;

        local _v = redis.call('DEL', _orz);
        if 1 ~= _v then
        return '';
        end;

        --
        _orz = 'goods:'.. _goods_id;

        _v = redis.call('DEL', _orz);
        if 1 ~= _v then
        return '';
        end;

        --
        return 'OK';
      """
    cmd = cache.register_script(lua_script)
    res = cmd(keys=[], args=[user_id, token, goods_id])
    print(res)
    return res


if __name__ == '__main__':
    create_goods_cache('1', 1000)
    create_token_cache('用户', 'token', 1000)
    snap_up_cache('用户', 'token', '1')
